package com.example.demo.controller;


import com.example.demo.entity.dto.TokenUserDto;
import com.example.demo.entity.vo.ResponseVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class BaseController {

    @Autowired
    private RedisTemplate redisTemplate;



    protected static final String STATUC_SUCCESS = "success";

    protected static final String STATUC_ERROR = "error";


    protected <T> ResponseVO getSuccess(String msg,T t) {
        ResponseVO<T> responseVO = new ResponseVO<>();
        responseVO.setStatus(STATUC_SUCCESS);
        responseVO.setCode(200);
        responseVO.setInfo(msg);
        responseVO.setData(t);
        return responseVO;
    }


    protected <T> ResponseVO getFail(String msg) {
        ResponseVO vo = new ResponseVO();
        vo.setStatus(STATUC_ERROR);
        vo.setCode(444);
        vo.setInfo(msg);
        vo.setData(null);
        return vo;
    }

    protected TokenUserDto getTokenUserDto(HttpServletRequest request){
        String token = request.getHeader("authorization");
        return (TokenUserDto) redisTemplate.opsForValue().get("token:"+token.substring(7));
    }

}
